package ${basePackage}.cli.command;
import ${basePackage}.generator.MainGenerator;
import ${basePackage}.model.DataModel;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import cn.hutool.core.bean.BeanUtil;
import lombok.Data;
import java.util.concurrent.Callable;


<#macro generatorOption modelInfo indent>
${indent}@Option(names = {<#if modelInfo.abbr??>"-${modelInfo.abbr}"</#if>,<#if modelInfo.fieldName??>"--${modelInfo.fieldName}</#if>" },echo = true, <#if modelInfo.description??>description = "${modelInfo.description}",</#if> arity = "0..1", interactive = true)
${indent}private ${modelInfo.type} ${ modelInfo.fieldName} <#if modelInfo.defaultValue??>= ${modelInfo.defaultValue?c}</#if>;
</#macro>

<#macro generatorCommand modelInfo indent>
${indent}System.out.println("输入${modelInfo.groupName}配置");
${indent}CommandLine commandLine = new CommandLine(${modelInfo.type}Command.class);
${indent}commandLine.execute(${modelInfo.allArgsStr});
</#macro>

/**
 * @author ${author}
 * @version ${version}
 * @description TODO
 * @date 2024/7/24 12:52
 */
@Command(name = "generator", mixinStandardHelpOptions = true)
public class GenerateCommand implements Callable<Integer> {
    <#list modelConfig.models as modelInfo>
        <#if modelInfo.groupKey??>
        /*
        * ${modelInfo.groupName}
        */
        static DataModel.${modelInfo.type} ${modelInfo.groupName} = new DataModel.${modelInfo.type}();
        @Command(name = "${modelInfo.groupName}", description = "${modelInfo.description}")
        @Data
        public static class ${modelInfo.type}Command implements Runnable {
            <#list modelInfo.models as SubModelInfo>
                <@generatorOption modelInfo=SubModelInfo indent="        "/>
            </#list>

            @Override
            public void run() {
            <#list modelInfo.models as SubModelInfo>
            ${modelInfo.groupName}.${SubModelInfo.fieldName} = ${SubModelInfo.fieldName};
            </#list>
            }
        }
            <#else>
        <@generatorOption modelInfo=modelInfo indent="    "/>
        </#if>

    </#list>



    @Override
    public Integer call() throws Exception {
       <#list modelConfig.models as modelInfo>
           <#if modelInfo.groupKey??>
           <#if modelInfo.condition??>
           if(${modelInfo.condition}){
               <@generatorCommand modelInfo=modelInfo indent="            "  />
           }
           <#else>
           <@generatorCommand modelInfo=modelInfo indent="            "  />
           </#if>
           </#if>
       </#list>
<#--       填充数据-->
       DataModel dataModel = new DataModel();
       BeanUtil.copyProperties(this,dataModel);
       <#list modelConfig.models as modelInfo>
       <#if modelInfo.groupKey??>
       dataModel.${modelInfo.groupKey} = ${modelInfo.groupName};
       </#if>
       </#list>
        MainGenerator.doGenerate(dataModel);
        return 0;
    }
}
